home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Demos / DuelVoice / diutil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  4.7 KB  |  170 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DIUtil.cpp
  3. //
  4. // Desc: Input routines
  5. //
  6. // Copyright (C) 1995-2001 Microsoft Corporation. All Rights Reserved.
  7. //-----------------------------------------------------------------------------
  8. #include "duel.h"
  9. #include "diutil.h"
  10. #include "gameproc.h"
  11.  
  12.  
  13. //-----------------------------------------------------------------------------
  14. // Globals
  15. //-----------------------------------------------------------------------------
  16. static LPDIRECTINPUT       g_pDI;               // DirectInput interface
  17. static LPDIRECTINPUTDEVICE g_pdidKeyboard;      // Keyboard device interface
  18. static BOOL                g_bKeyboardAcquired; // Whether eyboard is acquired
  19.  
  20.  
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Name: DIUtil_InitInput()
  25. // Desc: Initialize DirectInput objects & devices
  26. //-----------------------------------------------------------------------------
  27. HRESULT DIUtil_InitInput( HWND hWnd )
  28. {
  29.     // Create DI object
  30.     HINSTANCE hInst;
  31. #ifdef _WIN64
  32.     hInst = (HINSTANCE)GetWindowLongPtr( hWnd, GWLP_HINSTANCE );
  33. #else
  34.     hInst = (HINSTANCE)GetWindowLong( hWnd, GWL_HINSTANCE );
  35. #endif
  36.     if( FAILED( DirectInputCreate( hInst,
  37.                                    DIRECTINPUT_VERSION, &g_pDI, NULL ) ) )
  38.     {
  39.         ShowError(IDS_DINPUT_ERROR_DIC);
  40.         return E_FAIL;
  41.     }
  42.  
  43.     // Create keyboard device
  44.     if( FAILED( g_pDI->CreateDevice( GUID_SysKeyboard, &g_pdidKeyboard, NULL ) ) )
  45.     {
  46.         ShowError(IDS_DINPUT_ERROR_CD);
  47.         return E_FAIL;
  48.     }
  49.  
  50.     // Tell DirectInput that we want to receive data in keyboard format
  51.     if( FAILED( g_pdidKeyboard->SetDataFormat( &c_dfDIKeyboard) ) )
  52.     {
  53.         ShowError(IDS_DINPUT_ERROR_DF);
  54.         return E_FAIL;
  55.     }
  56.  
  57.     // Set cooperative level
  58.     if( FAILED( g_pdidKeyboard->SetCooperativeLevel( hWnd,
  59.                                   DISCL_NONEXCLUSIVE | DISCL_FOREGROUND ) ) )
  60.     {
  61.         ShowError(IDS_DINPUT_ERROR_SP);
  62.         return E_FAIL;
  63.     }
  64.  
  65.     // try to acquire the keyboard
  66.     if( SUCCEEDED( g_pdidKeyboard->Acquire() ) )
  67.         g_bKeyboardAcquired = TRUE;
  68.     else
  69.         g_bKeyboardAcquired = FALSE;
  70.  
  71.     return S_OK;
  72. }
  73.  
  74.  
  75.  
  76.  
  77. //-----------------------------------------------------------------------------
  78. // Name: DIUtil_ReadKeys()
  79. // Desc: Use DirectInput to read game-play keys
  80. //-----------------------------------------------------------------------------
  81. VOID DIUtil_ReadKeys( DWORD* pdwKeys )
  82. {
  83.     BYTE    rgbKeybd[256];
  84.     DWORD   dwKeys = 0L;
  85.     HRESULT hr;
  86.  
  87.     hr = g_pdidKeyboard->GetDeviceState( sizeof(rgbKeybd), rgbKeybd );
  88.     if( FAILED(hr) )
  89.     {
  90.         if( hr == DIERR_INPUTLOST )
  91.         {
  92.             // We lost control of the keyboard, reacquire
  93.             if( SUCCEEDED( g_pdidKeyboard->Acquire() ) )
  94.                 g_bKeyboardAcquired = TRUE;
  95.             else
  96.                 g_bKeyboardAcquired = FALSE;
  97.         }
  98.  
  99.         // Failed to read the keyboard, just return
  100.         return;
  101.     }
  102.  
  103.     // check & update key states
  104.     if( rgbKeybd[DIK_NUMPAD5] & 0x80 )
  105.         dwKeys |= KEY_STOP;
  106.  
  107.     if( (rgbKeybd[DIK_NUMPAD2] & 0x80) || (rgbKeybd[DIK_DOWN] & 0x80) )
  108.         dwKeys |= KEY_DOWN;
  109.  
  110.     if( (rgbKeybd[DIK_NUMPAD4] & 0x80) || (rgbKeybd[DIK_LEFT] & 0x80) )
  111.         dwKeys |= KEY_LEFT;
  112.  
  113.     if( (rgbKeybd[DIK_NUMPAD6] & 0x80) || (rgbKeybd[DIK_RIGHT] & 0x80) )
  114.         dwKeys |= KEY_RIGHT;
  115.  
  116.     if( (rgbKeybd[DIK_NUMPAD8] & 0x80) || (rgbKeybd[DIK_UP] & 0x80) )
  117.         dwKeys |= KEY_UP;
  118.  
  119.     if( rgbKeybd[DIK_SPACE] & 0x80 )
  120.         dwKeys |= KEY_FIRE;
  121.  
  122.     // Return the keys
  123.     (*pdwKeys) = dwKeys;
  124. }
  125.  
  126.  
  127.  
  128.  
  129. //-----------------------------------------------------------------------------
  130. // Name: DIUtil_CleanupInput()
  131. // Desc: Cleans up DirectInput objects
  132. //-----------------------------------------------------------------------------
  133. VOID DIUtil_CleanupInput()
  134. {
  135.     if(g_bKeyboardAcquired)
  136.     {
  137.         g_pdidKeyboard->Unacquire();
  138.         g_bKeyboardAcquired = FALSE;
  139.     }
  140.  
  141.     if( g_pdidKeyboard )
  142.         g_pdidKeyboard->Release();
  143.  
  144.     if( g_pDI )
  145.         g_pDI->Release();
  146. }
  147.  
  148.  
  149.  
  150.  
  151. //-----------------------------------------------------------------------------
  152. // Name: DIUtil_ReacquireInputDevices()
  153. // Desc: Reacquires DirectInput devices as needed
  154. //-----------------------------------------------------------------------------
  155. HRESULT DIUtil_ReacquireInputDevices()
  156. {
  157.     g_bKeyboardAcquired = FALSE;
  158.  
  159.     if( NULL == g_pdidKeyboard )
  160.         return E_FAIL;
  161.  
  162.     g_pdidKeyboard->Acquire();
  163.     g_bKeyboardAcquired = TRUE;
  164.     
  165.     return S_OK;
  166. }
  167.  
  168.  
  169.  
  170.